<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="150px">
        <div class="title-bar">
          <img class="logo" src="../assets/LOGO.png" alt="logo" />
          <div class="title">明凡云</div>
        </div>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="80px">
        <div class="user-login-status">
          <a-dropdown
            v-if="loginUserStore.loginUser.id"
            placement="bottomRight"
          >
            <a class="user-name" @click="(e) => e.preventDefault()">
              <a-avatar
                :src="loginUserStore.loginUser.avatarUrl"
                :alt="loginUserStore.loginUser.username ?? '用户'"
                size="small"
                style="margin-right: 2px"
              >
                {{ loginUserStore.loginUser.username?.charAt(0) ?? "用" }}
              </a-avatar>
              {{ loginUserStore.loginUser.username ?? "无名" }}
            </a>
            <template #overlay>
              <a-menu>
                <a-menu-item key="logout" @click="handleLogout">
                  <LogoutOutlined />
                  注销登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
          <div v-else>
            <a-button type="primary" href="/user/login"> 登录 </a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { h, ref } from "vue";
import {
  CrownOutlined,
  HomeOutlined,
  LogoutOutlined,
} from "@ant-design/icons-vue";
import { MenuProps, message } from "ant-design-vue";
import { useRouter } from "vue-router";
import { useLoginUserStore } from "@/store/useLoginUserStore";
import { userLogout } from "@/api/user";
const loginUserStore = useLoginUserStore();
const router = useRouter();

/**
 * 处理注销登录
 */
const handleLogout = async () => {
  try {
    const res = await userLogout({});
    if (res.data.code === 0) {
      // 清除本地登录状态
      loginUserStore.loginUser = {};
      message.success("注销成功");
      // 跳转到首页
      router.push({
        path: "/",
        replace: true,
      });
    } else {
      message.error("注销失败");
    }
  } catch (error) {
    message.error("注销失败，请重试");
  }
};

// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  });
};

const current = ref<string[]>(["mail"]);
router.afterEach((to, from, failure) => {
  current.value = [to.path];
});
const items = ref<MenuProps["items"]>([
  {
    key: "/",
    icon: () => h(HomeOutlined),
    label: "主页",
    title: "主页",
  },
  {
    key: "/roompage/CountdownPage",
    label: "倒计时",
    title: "倒计时",
  },
  {
    key: "",
    label: "用户操作",
    title: "用户操作",
    children: [
      {
        key: "/user/login",
        label: "用户登录",
        title: "用户登录",
      },
      {
        key: "/user/register",
        label: "用户注册",
        title: "用户注册",
      },
      {
        key: "/admin/userManage",
        icon: () => h(CrownOutlined),
        label: "用户管理",
        title: "用户管理",
      },
    ],
  },
  {
    key: "",
    label: "工具",
    title: "工具",
    children: [
      {
        key: "/tools/mdToolViewPage",
        label: "MD编辑器",
      },
      {
        key: "/tools/videoParsToolPage",
        label: "视频解析",
      },
      {
        key: "/roompage/CommunicationPage",
        label: "交流大厅",
      },
    ],
  },
  {
    key: "other",
    label: h(
      "a",
      { href: "https://mingfancloud.cn", target: "_blank" },
      "明凡编程宝典"
    ),
    title: "明凡编程宝典",
  },
  {
    key: "/others",
    label: h(
      "a",
      { href: "https://download.mingfancloud.cn", target: "_blank" },
      "明凡Download"
    ),
    title: "明凡Download",
  },
]);
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 30px;
}

.user-login-status {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.user-name {
  color: #333;
  font-weight: 500;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 2px;
}

.user-name:hover {
  background-color: #f5f5f5;
  color: #1890ff;
}
</style>
